package com.bjpowernode.rbac.web.controller;

import com.bjpowernode.rbac.anno.RequestPermission;
import com.bjpowernode.rbac.entity.*;
import com.bjpowernode.rbac.query.CustomerQuery;
import com.bjpowernode.rbac.query.HistoryQuery;
import com.bjpowernode.rbac.service.CustomerTraceHistoryService;
import com.bjpowernode.rbac.service.CustomerTransferService;
import com.bjpowernode.rbac.service.SystemDictionaryItemService;
import com.bjpowernode.rbac.util.DateUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/12 17:04
 */
@Controller
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    private CustomerTraceHistoryService customerTraceHistoryService;

    @Autowired
    private CustomerTransferService customerTransferService;

    @Autowired
    private SystemDictionaryItemService systemDictionaryItemService;

    /**
     * 转到跟进历史管理页面
     * @return
     */
    @RequestMapping("/toTraceHistoryPage")
    @RequestPermission({"跟进历史页面查询权限","history:toTraceHistoryPage"})
    public String toTraceHistoryPage(Model model, @ModelAttribute("qo") HistoryQuery historyQuery){
        historyQuery.setPageSize(2);
        //调用业务层获取数据
        PageInfo<CustomerTraceHistory> pageInfo = customerTraceHistoryService.selectTraceHistoryByCondition(historyQuery);

        List<SystemDictionaryItem> traceTypeIds = systemDictionaryItemService.selectAllTraceTypeIds();
        model.addAttribute("traceTypeIds",traceTypeIds);

        List<SystemDictionaryItem> types = systemDictionaryItemService.selectAllTypes();
        model.addAttribute("types",types);

        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("pageInfoList",pageInfo.getList());
        return "history/tracehistorypage";
    }

    /**
     * 跟进历史编辑
     * @param customerTraceHistory
     * @return
     */
    @RequestMapping("/update")
    @RequestPermission({"跟进历史编辑权限","history:update"})
    public String update(CustomerTraceHistory customerTraceHistory,String traceTime){
        customerTraceHistoryService.update(customerTraceHistory,traceTime);
        return "redirect:/history/toTraceHistoryPage";
    }


    /**
     * 转到客户移交历史页面
     * @return
     */
    @RequestMapping("/toTransferPage")
    @RequestPermission({"客户移交页面查询权限","history:toTransferPage"})
    public String toTransferPage(Model model, @ModelAttribute("qo") HistoryQuery historyQuery){
        historyQuery.setPageSize(5);
        PageInfo<CustomerTransfer> pageInfo = customerTransferService.selectTransferByCondiTion(historyQuery);
        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        return "history/transferpage";
    }

}
